package com.linhh.mall.center.web.filter;

import com.linhh.mall.center.api.UserInfoService;
import com.linhh.mall.center.common.pojo.UserDetailsImpl;
import com.linhh.mall.center.common.pojo.UserInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户信息查询
 * @Author linhh
 * @Date 2022 10 19 14 17
 **/
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Reference
    private UserInfoService userInfoService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserDetailsImpl userDetails = new UserDetailsImpl();
        UserInfo userInfo = userInfoService.findByUserName(username);
        if (userInfo == null) {
            throw new UsernameNotFoundException(username + "用户不存在");
        }
        userDetails.setUserName(username);
        userDetails.setUserPassword(userInfo.getUserPassword());
        return userDetails;
    }
}
